﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.Equipment.common
{
    public partial class Location : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定所属实验室
                LabDropdowlist();

                //绑定数据gridview
                LabRoom_BindData(0, "", "", "", "", "");
            }

        }

        //绑定LabRoomGridView
        protected void LabRoom_BindData(int LabID, string LocalName, string Campus, string BuildingName, string Floor, string RoomNum)
        {
            ExtendBLL.Location lc = new ExtendBLL.Location();
            DataSet ds = new DataSet();

            ds = lc.GetRoomInfo(LabID, LocalName, Campus, BuildingName, Floor, RoomNum);
            LabRoomGridView.DataSource = ds;
            LabRoomGridView.DataBind();
        }

        protected void LabRoomGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
            {
             
            }
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("OnDblClick", "DbClickEvent('"+LabRoomGridView.DataKeys[LabRoomGridView.DataKeys.Count - 1].Value.ToString() + "','"+ e.Row.Cells[1].Text +"')");
                e.Row.Attributes["style"] = "Cursor:pointer";

            }


        }

        //删除实验用房信息
        protected void Delete_OnClick(object sender, EventArgs e)
        {
            ExtendBLL.Location loc = new ExtendBLL.Location();
            bool IsDel = false;
            for (int i = 0; i < LabRoomGridView.Rows.Count; i++)
            {
                CheckBox checkBox = (CheckBox)LabRoomGridView.Rows[i].FindControl("RoomCheckBox");

                if (checkBox.Checked == true)
                {
                    IsDel = true;
                    int ID = int.Parse(LabRoomGridView.DataKeys[i].Value.ToString());
                    List<string> strID = new List<string>();
                    List<string> strTable = new List<string>();
                    List<string> strColName = new List<string>();
                    #region 用房
                    strID.Add(ID.ToString());
                    strTable.Add("Location");
                    strColName.Add("ID");
                    #endregion
                    #region 课程安排
                    strID.Add(ID.ToString());
                    strTable.Add("Arrangement");
                    strColName.Add("LocationID");
                    #endregion
                    try
                    {
                        loc.DeleteLocation(strID, strTable, strColName);

                    }
                    catch
                    {
                        Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script type='text/javascript'>alert('删除失败!')</script>");
                        return;
                    }
                }

            }
            if (IsDel)
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script type='text/javascript'>alert('删除成功!')</script>");
                LabRoom_BindData(int.Parse(LabDDL.SelectedValue), LocationNameTBX.Text, CampusTBX.Text, BuildingNameTBX.Text, FloorTBX.Text, RoomNumTBX.Text);
            }
        }

        //绑定所属实验室
        protected void LabDropdowlist()
        {
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> ParLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> SonLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> Lab = new List<LabMS.Model.Lab>();

            Lab = lab.GetModelList("Lab_ParentID is null");
            ParLab = lab.GetModelList("Lab_ParentID is null");
            SonLab = lab.GetModelList("Lab_ParentID is not null");
            int count = ParLab.Count, count1 = SonLab.Count;

            for (int i = 0; i < count; i++)
            {
                int ID = ParLab[i].ID;
                for (int j = 0; j < count1; j++)
                {
                    LabMS.Model.Lab labModel = new LabMS.Model.Lab();
                    int ParentID = int.Parse(SonLab[j].Lab_ParentID.ToString());

                    if (ParentID == ID)
                    {
                        labModel.ID = SonLab[j].ID;
                        labModel.Lab_Name = "|-" + SonLab[j].Lab_Name;
                        Lab.Insert(i + j + 1, labModel);
                    }
                }
            }
            LabDDL.DataSource = Lab;
            LabDDL.DataBind();
            ListItem item = new ListItem("--请选择实验室--", "0");
            LabDDL.Items.Insert(0, item);
        }

        // 分页
        protected void LabRoomGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            LabRoomGridView.PageIndex = e.NewPageIndex;
            LabRoom_BindData(int.Parse(LabDDL.SelectedValue), LocationNameTBX.Text, CampusTBX.Text, BuildingNameTBX.Text, FloorTBX.Text, RoomNumTBX.Text);
        }

        //搜索
        protected void Search_OnClick(object sender, EventArgs e)
        {
            LabRoom_BindData(int.Parse(LabDDL.SelectedValue), LocationNameTBX.Text, CampusTBX.Text, BuildingNameTBX.Text, FloorTBX.Text, RoomNumTBX.Text);
        }

        protected void DropDownListIndex_OnChanged(object sender, EventArgs e)
        {
            ExtendBLL.Location location = new ExtendBLL.Location();
            DataSet ds = new DataSet();
            int LabID = 0;
            if (LabDDL.SelectedValue != "0")
            {
                LabID = int.Parse(LabDDL.SelectedValue);
            }
            ds = location.GetRoomInfo(LabID, LocationNameTBX.Text, CampusTBX.Text, BuildingNameTBX.Text, FloorTBX.Text, RoomNumTBX.Text);
            LabRoomGridView.DataSource = ds;
            LabRoomGridView.DataBind();
        }
    }
}
